import store from '../../store/index'
import { Message, Loading } from 'element-ui';
import globalUser from './global_user'
import Vue from 'vue'

let loading        //定义loading变量
var bus = new Vue()
const commonFn = {
  getBus(){
    return bus
  },
    j2s(obj) {
        return JSON.stringify(obj)
    },
    closeGlobalLoading() {
      if(loading){
        loading.close()
        loading = null
      }
    },
  openGlobalLoading(target) {
    if(!loading){
      if(target){
        loading = Loading.service({
          target: document.querySelector(target),
          lock: true,
          text: '加载中……',
          fullscreen: true,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      }else{
        loading = Loading.service({
          target: document.querySelector('.panel-c-c'),
          lock: true,
          text: '加载中……',
          fullscreen: true,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      }
    }
  },
    openFullScreen() {
        const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el_icon_loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        setTimeout(() => {
            loading.close()
        }, 2000)
    },
    cloneJson(obj) {
        return JSON.parse(JSON.stringify(obj))
    },
    isString(obj) { // 判断对象是否是字符串
        return Object.prototype.toString.call(obj) === '[object String]'
    },
    isArray(obj) { // 判断对象是否是数组
        return Object.prototype.toString.call(obj) === '[object Array]'
    },
    isNumber(obj) { // 判断对象是否是数字
        return Object.prototype.toString.call(obj) === '[object Number]'
    },
    clearVuex(cate) {
        store.dispatch(cate, [])
    },
    bzeroObj(obj){
      for(key in obj){
        obj[key] = null
      }
    },
  clone(obj) {
    let o;
    if (typeof obj == "object") {
      if (obj === null) {
        o = null;
      } else {
        if (obj instanceof Array) {
          o = [];
          for (var i = 0, len = obj.length; i < len; i++) {
            o.push(clone(obj[i]));
          }
        } else {
          o = {};
          for (var j in obj) {
            o[j] = this.clone(obj[j]);
          }
        }
      }
    } else {
      o = obj;
    }
    return o;
  },
  showMsg(msg, type){
    Message({
      message: msg,
      type: type
    })
  },
  
  
    
    /*用户相关的全局函数*/
  getUserNameFromStorage() {
        return globalUser.getUserNameFromStorage()
    },
  getUserNameFromMemory(){
      return globalUser.getUserNameFromMemery()
  },
  setUserName2Memory(name){
      globalUser.setUserName2Memory(name)
  },
  setUserName2Storage(name){
      globalUser.setUserName2Storage(name)
  },
  removeUserName(){
    globalUser.clearUserName()
  },
  //缓存用户相关信息至内存及本地
  saveUserInfo(response){
    globalUser.saveUserInfo(response)
  },
  clearUserInfo(){
    globalUser.clearUserInfo()
  },
    
    //  是否有验证码显示
    getHasVarifyCode() {
        return 0
    },
    //  系统名称
    getSystemName() {
        return '交通违章前置管理系统'
    },
    getRoleTypeName(type) {
        let ret = '未知'
        switch(type) {
            case 'A':
                ret = '系统管理'
                break
            case 'B':
                ret = '安全管理'
                break
            case 'C':
                ret = '业务管理'
                break
            case 'D':
                ret = '审计管理'
                break
            default:
        }
        return ret
    },

    getPermissionNameList(permissionStrs){
        let arr = permissionStrs.split(",")
        let res = ""
        for(let i=0; i<arr.length; i++){
            res += this.getPermissionTypeName(arr[i])
            res += "、"
        }
        return res;
    },

    getPermissionTypeName(perid) {
        let ret = '未知权限'
        switch (perid) {
            case 'A0001':
                ret = '角色创建'
                break
            case 'A0002':
                ret = '角色查询'
                break
            case 'A0003':
                ret = '角色修改'
                break
            case 'A0004':
                ret = '角色删除'
                break
            case 'A0005':
                ret = '用户创建'
                break
            case 'A0006':
                ret = '用户查询'
                break
            case 'A0007':
                ret = '用户修改'
                break
            case 'A0008':
                ret = '用户删除'
                break
            case 'A0009':
                ret = '权限查询'
                break
            case 'A0010':
                ret = '权限修改'
                break
            case 'A0011':
                ret = '用户激活状态设置'
                break
            case 'A0012':
                ret = '用户锁定状态管理'
                break
            case 'A0013':
                ret = '用户终端绑定'
                break
            case 'A0014':
                ret = '用户激活状态查询'
                break
            case 'B0001':
                ret = '用户登录失败次数阈值设置'
                break
            case 'B0002':
                ret = '终端登录失败次数阈值设置'
                break
            case 'B0003':
                ret = '用户有效期设定'
                break
            case 'B0004':
                ret = '密码有效期设定'
                break
            case 'B0005':
                ret = '账户长期未使用阈值设置'
                break
            case 'B0006':
                ret = '用户允许登录时间段设置'
                break
            case 'B0007':
                ret = '系统最大在线数设置'
                break
            case 'B0008':
                ret = '用户高频登录阈值设置'
                break
            case 'B0009':
                ret = '会话超时阈值设置'
                break
            case 'B0010':
                ret = '终端锁定状态设置'
                break

            case 'D0001':
                ret = '用户登录日志查询'
                break
            case 'D0002':
                ret = '用户安全日志查询'
                break
            case 'D0003':
                ret = '用户操作日志查询'
                break
            case 'D0004':
                ret = '安全日志报表分析'
                break
            case 'D0005':
                ret = '操作日志报表分析'
                break
            case 'D0006':
                ret = '登录日志报表分析'
                break
            case 'D0007':
                ret = '登录日志存储期限设置'
                break
            case 'D0008':
                ret = '操作日志存储期限设置'
                break
            case 'D0009':
                ret = '安全日志存储期限设置'
                break
            case 'D0010':
                ret = '会话报文日志存储期限设置'
                break

            case 'C0001':
                ret = '驾驶人信息查询'
                break
            case 'C0002':
                ret = '驾驶证信息查询'
                break
            case 'C0003':
                ret = '机动车信息查询'
                break
            case 'C0004':
                ret = '处罚书查询处理'
                break
            case 'C0005':
                ret = '电子警察查询处理'
                break
            case 'C0006':
                ret = '银行对账信息查询'
                break
            case 'C0007':
                ret = '驾驶人车辆核查'
                break
            case 'C0008':
                ret = '违法行为代码查询'
                break
            case 'C0009':
                ret = '业务操作日志查询'
                break
            case 'C0010':
                ret = '决定书处理日志查询'
                break
        }
        return ret

    },

    //返回权限列表的中文数组
    getPermListFromStr(str) {
        if ((str == null)) {
            return []
        }
        var perList = []
        perList = str.split(',')
        for (var i in perList) {
            perList[i] = this.getPermissionTypeName(perList[i])
        }
        return perList
    },

    //返回权限列表的对象数组 [{id: 'A0001', name: ''}]
    getPermListObjFromStr(str) {
        if((str == null)) {
            return []
        }
        var perList = []
        var perListObj = []
        perList = str.split(',')
        for (var i in perList) {
            perListObj[i].id = perList[i]
            perListObj[i].name = this.getPermissionTypeName(perList[i])
        }
        return perListObj
    },

    // logo type = 1 图片logo type = 0 文字logo
    getLogoType() {
        return 1
    },
    // menuData是总menu的child
    // 从menuJson中找到第一个status==1的子菜单,返回这个菜单的child内全部数据
    // 我默认菜单权限数据是合理的，即母菜单的【权限无】和子菜单的【权限有】是不会出现的。!!![wj:健壮性=垃圾级]
    // 代码真是ugly，用递归其实更好
    getFirstAvailableSubMenu(menuData) {
        for (var i in menuData) {
            if (menuData[i].status != 1) {
                continue
            } else {
                for (var j in menuData[i].child) {
                    if (menuData[i].child[j].status != 1) {
                        continue
                    } else {
                        return menuData[i].child[j]
                    }
                }
            }
        }
    },

    mkRslt(arr) {
        var arrRslt = ['']
        for (var i = 0, len = arr.length; i < len; i++) {
            var str = arr[i]
            var strlen = str.length
            if (strlen == 1) {
                for (var k = 0; k < arrRslt.length; k++) {
                    arrRslt[k] += str
                }
            } else {
                var tmpArr = arrRslt.slice(0)
                arrRslt = []
                for (k = 0; k < strlen; k++) {
                    // 复制一个相同的arrRslt
                    var tmp = tmpArr.slice(0)
                    // 把当前字符str[k]添加到每个元素末尾
                    for (var j = 0; j < tmp.length; j++) {
                        tmp[j] += str.charAt(k)
                    }
                    // 把复制并修改后的数组连接到arrRslt上
                    arrRslt = arrRslt.concat(tmp)
                }
            }
        }
        return arrRslt
    },

    queryHelpName(str) {
        String.prototype.trim = function () {
            return this.replace(/(^\s*)|(\s*$)/g, '')
        }
        if (str == '') return
        if (typeof (str) != 'string') {
            throw new Error(-1, '函数makePy需要字符串类型参数!')
        }
        var arrResult = new Array() // 保存中间结果的数组
        for (var i = 0, len = str.length; i < len; i++) {
            // 获得unicode码
            var ch = str.charAt(i)
            // 检查该unicode码是否在处理范围之内,在则返回该码对映汉字的拼音首字母,不在则调用其它函数处理
            arrResult.push(this.checkCh(ch))
        }
        // 处理arrResult,返回所有可能的拼音首字母串数组
        return this.mkRslt(arrResult)
    }
}

export default commonFn
